package com.gopay.common.exception;

import com.gopay.common.constants.proccode.ProcCodeEnum;
import org.apache.commons.lang.StringUtils;

/**
 * 国付宝异常类
 * 
 * @GopayException.java
 * @author yaobo
 * @2012-12-19 上午9:55:51 www.gopay.com.cn Inc.All rights reserved.
 */
public class GopayException extends Exception {

    /**
     * 
     */
    private static final long serialVersionUID = 5088270223274078374L;

    /**
     * 模块名，抛出异常的类
     */
    @SuppressWarnings("rawtypes")
    protected Class errClass = null;

    /**
     * 错误码
     */
    protected String errCode = null;

    /**
     * 错误描述信息
     */
    protected String errMsg = null;

    /**
     * 包装不对外异常信息
     */
    protected String debugMsg = null;

    public GopayException() {
        super();
    }

    public GopayException(String errCode) {
        super("no errMsg");
        this.errCode = errCode;
    }

    public GopayException(String errCode, Exception e) {
        super("no errMsg", e);
        this.errCode = errCode;
    }

    public GopayException(String errCode, String errMsg) {
        super(errMsg);
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public GopayException(String errCode, String errMsg , String debugMsg) {
        super(errMsg+"（"+debugMsg+"）");
        this.errCode = errCode;
        this.errMsg = errMsg;
        this.debugMsg = debugMsg;
    }

    public GopayException(ProcCodeEnum procCodeEnum) {
        super(procCodeEnum.getMsg());
        this.errCode = procCodeEnum.getCode();
        this.errMsg = procCodeEnum.getMsg();
    }

    public GopayException(ProcCodeEnum procCodeEnum ,Exception e) {
        super(procCodeEnum.getMsg(),e);
        this.errCode = procCodeEnum.getCode();
        this.errMsg = procCodeEnum.getMsg();
    }

    public GopayException(String errCode, String errMsg, Exception e) {
        super(errMsg, e);
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public GopayException(ProcCodeEnum procCodeEnum , String debugMsg, Exception e) {
        super(procCodeEnum.getMsg()+"（"+debugMsg+"）",e);
        this.errCode = procCodeEnum.getCode();
        this.errMsg = procCodeEnum.getMsg();
        this.debugMsg = debugMsg;
    }

    public GopayException(ProcCodeEnum procCodeEnum, String debugMsg) {
        super(procCodeEnum.getMsg()+"（"+debugMsg+"）");
        this.errCode = procCodeEnum.getCode();
        this.errMsg = procCodeEnum.getMsg();
        this.debugMsg = debugMsg;
    }

    /**
     * 封装对商户不可见的异常
     * procCodeEnum 对外包装的异常
     * procCodeInnerEnum：对内暴露的明确异常
     * @author <a href="mailto:yhwpeng@126.com">wangdong</a>
     * @date 2017-09-29 15:37:59
     */
    public GopayException(ProcCodeEnum procCodeEnum, ProcCodeEnum procCodeInnerEnum) {
        super(procCodeEnum.getMsg()+"（"+procCodeInnerEnum.getMsg()+"）");
        this.errCode = procCodeEnum.getCode();
        this.errMsg = procCodeEnum.getMsg();
        this.debugMsg = procCodeInnerEnum.getMsg();
    }

    /**
     * 封装对商户不可见的异常
     * procCodeEnum 对外包装的异常
     * procCodeInnerEnum：对内暴露的明确异常
     * debugMsg：错误真实原因
     * @author <a href="mailto:yhwpeng@126.com">wangdong</a>
     * @date 2017-09-29 15:37:59
     */
    public GopayException(ProcCodeEnum procCodeEnum, ProcCodeEnum procCodeInnerEnum, String debugMsg) {
        super(procCodeInnerEnum.getMsg()+"（"+debugMsg+"）");
        this.errCode = procCodeEnum.getCode();
        this.errMsg = procCodeEnum.getMsg();
        this.debugMsg = procCodeInnerEnum.getMsg()+"（"+debugMsg+"）";
    }

    @SuppressWarnings("rawtypes")
    public GopayException(String errCode, Class errClass) {
        super("no errMsg");
        this.errCode = errCode;
        this.errClass = errClass;
    }

    @SuppressWarnings("rawtypes")
    public GopayException(String errCode, String errMsg, Class errClass) {
        super(errMsg);
        this.errCode = errCode;
        this.errMsg = errMsg;
        this.errClass = errClass;
    }

    @SuppressWarnings("rawtypes")
    public GopayException(String errCode, String errMsg, Class errClass, Exception e) {
        super(errMsg, e);
        this.errClass = errClass;
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public String printErrMsg() {
        StringBuffer msg = new StringBuffer("");

        if (StringUtils.isNotBlank(errCode)) {
            msg.append("Exception errCode is [").append(this.errCode).append("];\n");
        }
        if (this.errClass != null) {
            msg.append("Exception Class   is [").append(this.errClass.getName()).append("];\n");
        }
        msg.append("Exception Message is [").append(this.errMsg).append("];\n");
        // 打印错误异常 堆栈
        if (getCause() != null) {
            getCause().printStackTrace();
        }

        return msg.toString();
    }

    @Override
    public String toString() {
        String s = getClass().getName() + ": " + errCode;
        String message = getLocalizedMessage();
        return (message != null) ? (s + ">>> " + message) : s;
    }

//    @Override
//    public String toString() {
//        StringBuffer sb = new StringBuffer();
//        if (org.apache.commons.lang3.StringUtils.isNotBlank(errCode)){
//            sb.append("errCode：");
//            sb.append(errCode);
//        }
//        if (org.apache.commons.lang3.StringUtils.isNotBlank(errMsg)){
//            sb.append(">>>errMsg：");
//            sb.append(errMsg);
//        }
//        if (org.apache.commons.lang3.StringUtils.isNotBlank(debugMsg)){
//            sb.append(">>>debugMsg：");
//            sb.append(debugMsg);
//        }
//        String message = getLocalizedMessage();
//        if (org.apache.commons.lang3.StringUtils.isNotBlank(message)){
//            sb.append(">>>exceptionMsg：");
//            sb.append(message);
//        }
//        return sb.toString();
//    }

    @SuppressWarnings("rawtypes")
    public Class getErrClass() {
        return errClass;
    }

    public String getErrCode() {
        return errCode;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public String getDebugMsg() {
        return debugMsg;
    }
}
